import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import * as express from 'express';
import * as serverless from 'aws-serverless-express';
import * as awsServerlessExpressMiddleware from 'aws-serverless-express/middleware';
import { Callback, Context, Handler } from 'aws-lambda';

const expressApp = express();
expressApp.use(awsServerlessExpressMiddleware.eventContext());
const server = serverless.createServer(expressApp);
// export const handler: Handler = async (event: any, context: Context, callback: Callback) => {
//   const app = await NestFactory.create(AppModule, expressApp);
//   await app.init();
//   const server = serverless.createServer(expressApp);
//
//   serverless.proxy(server, event, context);
// };

export const handler: Handler = (
  event: any,
  context: Context,
  callback: Callback,
) => {
  NestFactory.create(AppModule, expressApp).then(app => {
    app.init().then(() => {
      // context.succeed = (response) => {
      //   server.close();
      //   callback(null, response);
      // };
      return serverless.proxy(server, event, context);
    });
  });
};
